home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / httpd / cgi-src / annotate.c next >
C/C++ Source or Header  |  1995-05-09  |  3KB  |  116 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <time.h>
  5.  
  6. #define MAX_ENTRIES 5
  7.  
  8. #define DOCUMENT_ROOT "/usr/ftp/pub"
  9.  
  10. /* where you want to log any annotations made, undefine to turn off logging */
  11. #define LOGFILE "/usr/http/logs/annotate_log"
  12.  
  13. typedef struct {
  14.     char *name;
  15.     char *val;
  16. } entry;
  17.  
  18. char *makeword(char *line, char stop);
  19. char *fmakeword(FILE *f, char stop, int *len);
  20. char x2c(char *what);
  21. void unescape_url(char *url);
  22. void plustospace(char *str);
  23.  
  24. add_annotation(entry *entries)
  25. {
  26.     FILE *fp;
  27.     char filename[256];
  28.     time_t clock;
  29.  
  30.     sprintf(filename,"%s/annotations/%s",DOCUMENT_ROOT,entries[1].val);
  31.  
  32.     fp = fopen(filename, "a");
  33.     if (fp != NULL)
  34.     {
  35.     clock=time(0) ;
  36.         fprintf(fp, "<H3><HR>Annotation by %s at %s</H3>\n",entries[0].val,ctime(&clock));
  37.         fprintf(fp, "%s",entries[2].val);
  38.     }
  39.  
  40.     fclose(fp) ;
  41.  
  42. #ifdef LOGFILE
  43.  
  44.     fp = fopen(LOGFILE, "a");
  45.     if (fp != NULL)
  46.     {
  47.         fprintf(fp, "\n-----------------------------------------------------\n");
  48.         fprintf(fp, "%s: %s at %s\n",filename,entries[0].val,ctime(&clock));
  49.         fprintf(fp, "%s",entries[2].val);
  50.     }
  51.  
  52.     fclose(fp) ;
  53.  
  54. #endif
  55. }
  56.  
  57.  
  58. main(int argc, char *argv[]) {
  59.     entry entries[MAX_ENTRIES];
  60.     register int x,m=0;
  61.     int cl;
  62.     char filename[256];
  63.     char line[256];
  64.     FILE *fp;
  65.  
  66.     printf("Content-type: text/html%c%c",10,10);
  67.  
  68.     if(strcmp(getenv("REQUEST_METHOD"),"POST")) {
  69.         printf("This script should be referenced with a METHOD of POST.\n");
  70.         printf("If you don't understand this, see this ");
  71.         printf("<A HREF=\"http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html\">forms overview</A>.%c",10);
  72.         exit(1);
  73.     }
  74.     if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) {
  75.         printf("This script can only be used to decode form results. \n");
  76.         exit(1);
  77.     }
  78.     cl = atoi(getenv("CONTENT_LENGTH"));
  79.  
  80.     for(x=0;cl && (!feof(stdin));x++) {
  81.         m=x;
  82.         entries[x].val = fmakeword(stdin,'&',&cl);
  83.         plustospace(entries[x].val);
  84.         unescape_url(entries[x].val);
  85.         entries[x].name = makeword(entries[x].val,'=');
  86.     }
  87.  
  88.     if (entries[0].val[0] == '\0') {
  89.         printf("<TITLE>Annotation error</TITLE>\n") ;
  90.         printf("<H1>Annotation error</H1>\n") ;
  91.         printf("You did not enter a login! Please go back and try again.\n") ;
  92.          exit(0) ;
  93.     } 
  94.  
  95.     if (entries[2].val[0] == '\0') {
  96.         printf("<TITLE>Annotation error</TITLE>\n") ;
  97.         printf("<H1>Annotation error</H1>\n") ;
  98.         printf("You did not enter any comments! Please go back and try again.\n") ;
  99.          exit(0) ;
  100.     }
  101.  
  102.     add_annotation(entries) ;
  103.  
  104.     sprintf(filename,"%s/annotations/%s",DOCUMENT_ROOT,entries[1].val);
  105.  
  106.     fp = fopen(filename, "r");
  107.     if (fp != NULL)
  108.     {
  109.     while (fgets(line, 256, fp)!=NULL)
  110.         printf("%s",line) ;
  111.     }
  112.  
  113.     fclose(fp) ;
  114.  
  115. }
  116.